﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnATous.DataAccess
{
    public class Rating
    {
        public bool CreateRate(long idUser, long idContact, long idFeaturePrimary, long idFeatureSecondary, long idFeatureTertiary, int score, string comments, bool isVisible)
        {
            DBO.Rating rate = new DBO.Rating(idUser, idContact, idFeaturePrimary, idFeatureSecondary, idFeatureTertiary, score, comments, isVisible);

            try
            {
                OnATousEntities model = new OnATousEntities();
                model.AddToT_Rating(ToT_Rating(rate));
                model.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        public bool ModifyRate(DBO.Rating rate)
        {
            try
            {
                bool res;
                OnATousEntities model = new OnATousEntities();
                T_Rating t_rate = model.T_Rating.Where(x => x.Id == rate.Id).FirstOrDefault();
                if (t_rate != null)
                {
                    t_rate.Score = rate.Score;
                    t_rate.IdFeaturePrimary = rate.IdFeaturePrimary;
                    t_rate.IdFeatureSecondary = rate.IdFeatureSecondary;
                    t_rate.IdFeatureTertiary = rate.IdFeatureTertiary;
                    t_rate.Score = rate.Score;
                    t_rate.Comments = rate.Comments;
                    model.SaveChanges();
                    res = true;
                }
                else
                    res = CreateRate(rate.IdUser, rate.IdContact, rate.IdFeaturePrimary, rate.IdFeatureSecondary, rate.IdFeatureTertiary, rate.Score, rate.Comments, rate.IsVisible);
                
                return res;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public DBO.Rating GetRate(DBO.User user, DBO.User friend)
        {
            DBO.Rating rate;
            OnATousEntities model = new OnATousEntities();
            T_Rating t_rate = model.T_Rating.Where(x => x.IdUser == user.Id && x.IdContact == friend.Id).FirstOrDefault();
            if (t_rate != null)
                rate = new DBO.Rating(t_rate.IdUser, t_rate.IdContact, t_rate.IdFeaturePrimary, t_rate.IdFeatureSecondary, t_rate.IdFeatureTertiary, t_rate.Score, t_rate.Comments, t_rate.IsVisible);
            else
                rate = new DBO.Rating(user.Id, friend.Id, 1, 2, 3, 2, "", false);
            return rate;
        }

        public int GetNotationCount(DBO.User user)
        {
            OnATousEntities model = new OnATousEntities();
            int count = 0;
            count = model.T_Rating.Where(x => x.IdContact == user.Id).Count();
            return count;
        }

        private T_Rating ToT_Rating(DBO.Rating rate)
        {
            T_Rating t_rate = new T_Rating();
            if (rate.Id != 0)
                t_rate.Id = rate.Id;
            t_rate.IdUser = rate.IdUser;
            t_rate.IdContact = rate.IdContact;
            t_rate.IdFeaturePrimary = rate.IdFeaturePrimary;
            t_rate.IdFeatureSecondary = rate.IdFeatureSecondary;
            t_rate.IdFeatureTertiary = rate.IdFeatureTertiary;
            t_rate.Score = rate.Score;
            t_rate.Comments = rate.Comments;
            t_rate.IsVisible = rate.IsVisible;
            return t_rate;
        }

        public List<DBO.User> GetRatedContacts(DBO.User user)
        {
            List<DBO.User> res = new List<DBO.User>();
            OnATousEntities model = new OnATousEntities();

            List<T_Rating> list = model.T_Rating.Where(x => x.IdUser == user.Id).ToList();
            foreach (T_Rating rating in list)
            {
                T_User t_use = model.T_User.Where(x => x.Id == rating.IdContact).FirstOrDefault();
                DBO.User use = new DBO.User(t_use.Id, t_use.FirstName, t_use.LastName, t_use.Pseudo, 0, DateTime.MinValue);
                use.Credits = GetNotationCount(use);
                res.Add(use);
            }
            return res;
        }

        public List<DBO.User> GetNonRatedContacts(DBO.User user)
        {
            List<DBO.User> res = new List<DBO.User>();

            BusinessManagement.Facebook fbBM = new BusinessManagement.Facebook();
            List<DBO.User> friendlist = fbBM.GetFriendList();
            OnATousEntities model = new OnATousEntities();
            foreach (DBO.User friend in friendlist)
            {
                T_Rating t_rating = model.T_Rating.Where(x => x.IdUser == user.Id && x.IdContact == friend.Id).FirstOrDefault();
                if (t_rating == null)
                {
                    friend.Credits = GetNotationCount(friend);
                    res.Add(friend);
                }
            }
            return res;
        }

        public float GetMoyenne(DBO.User user)
        {
            int count = GetNotationCount(user);
            OnATousEntities model = new OnATousEntities();
            List<T_Rating> rates = model.T_Rating.Where(x => x.IdContact == user.Id).ToList();
            int sum = 0;
            foreach (T_Rating rate in rates)
            {
                sum += rate.Score;
            }
            if (count != 0)
                return (sum / count);
            return 0;
        }

        public List<int> GetListCountFeature(DBO.User user)
        {
            OnATousEntities model = new OnATousEntities();
            List<int> res = new List<int>();
            int i = 0;
            while (i < 10)
            {
                res.Add(model.T_Rating.Where(x => x.IdContact == user.Id && (x.IdFeaturePrimary == i + 1 || x.IdFeatureSecondary == i + 1 || x.IdFeatureTertiary == i + 1)).Count());
                i++;
            }
            return res;
        }

        public Dictionary<Int64, float> GetMarks()
        {
            DataAccess.User userDA = new DataAccess.User();

            OnATousEntities model = new OnATousEntities();
            List<T_Rating> t_rates = model.T_Rating.ToList();
            Dictionary<Int64, float> res = new Dictionary<long, float>();
            foreach (T_Rating t_rate in t_rates)
            {
                res.Add(t_rate.IdContact, GetMoyenne(userDA.GetUserById(t_rate.IdContact)));
            }
            return res;
        }
    }
}